package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.HomeBuilderDrawingCategoryAuditDTO;
import com.eastfair.venueservice.dto.HomeBuilderDrawingCategoryAuditPageQuery;
import com.eastfair.venueservice.dto.HomeBuilderDrawingCategoryAuditSaveDTO;
import com.eastfair.venueservice.dto.HomeBuilderDrawingCategoryAuditUpdateDTO;
import com.eastfair.venueservice.entity.HomeBuilderDrawingCategoryAudit;
import com.eastfair.venueservice.enumeration.HomeBuilderDrawingCategoryAuditDrawingSourceEnum;
import com.eastfair.venueservice.service.HomeBuilderDrawingCategoryAuditService;
import com.eastfair.venueservice.vo.HomeBuilderDrawingCategoryAuditVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 主场搭建商图纸分类审核记录
 * </p>
 *
 * @author dqq
 * @date 2023-04-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/homeBuilderDrawingCategoryAudit")
@Api(value = "HomeBuilderDrawingCategoryAudit", tags = "主场搭建商图纸分类审核记录")
@PreAuth(replace = "venueservice:homeBuilderDrawingCategoryAudit:")
public class HomeBuilderDrawingCategoryAuditController extends SuperController<HomeBuilderDrawingCategoryAuditService, Long, HomeBuilderDrawingCategoryAudit, HomeBuilderDrawingCategoryAuditPageQuery, HomeBuilderDrawingCategoryAuditSaveDTO, HomeBuilderDrawingCategoryAuditUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private HomeBuilderDrawingCategoryAuditService homeBuilderDrawingCategoryAuditService;

    @Override
    public void handlerResult(IPage<HomeBuilderDrawingCategoryAudit> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<HomeBuilderDrawingCategoryAudit> homeBuilderDrawingCategoryAuditList = list
                .stream()
                .map((map) -> HomeBuilderDrawingCategoryAudit.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(homeBuilderDrawingCategoryAuditList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<HomeBuilderDrawingCategoryAuditPageQuery> params) {
        HomeBuilderDrawingCategoryAuditPageQuery pageQuery = params.getModel();
    }

    /**
     * 分页查询图纸分类审核列表VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/query/drawingCategoryAudit")
    public R<Page<HomeBuilderDrawingCategoryAuditVO>> queryCategoryAudit(@RequestBody PageParams<HomeBuilderDrawingCategoryAuditDTO> pageParams) {
        log.info("queryCategoryAudit - 分页查询图纸分类审核列表VO, pageParams={}", pageParams);
        return R.successDef(homeBuilderDrawingCategoryAuditService.listCategoryAuditVO(pageParams));
    }

    /**
     * Save batch r.
     *
     * @param list the list
     * @return the r
     */
    @PostMapping("/saveBatch")
    public R<Boolean> saveBatch(@RequestBody List<HomeBuilderDrawingCategoryAuditDTO> list) {
        log.info("saveBatch - 批量保存图纸分类审核, list={}", list);
        try {
            return R.successDef(homeBuilderDrawingCategoryAuditService.submitBatchForBuilder(list));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 审核图纸分类.
     *
     * @param auditDTO the audit dto
     * @return the r
     */
    @SysLog("审核图纸")
    @PostMapping("/update/auditStatus")
    public R<Boolean> updateAuditStatus(@RequestBody HomeBuilderDrawingCategoryAuditDTO auditDTO) {
        log.info("updateAuditStatus - 审核图纸分类 - auditDTO={}", auditDTO);
        return R.successDef(homeBuilderDrawingCategoryAuditService.updateAuditStatus(auditDTO));
    }

    /**
     * 批量审核图纸.
     *
     * @param auditDTOList the audit dto list
     * @return the r
     */
    @SysLog("批量审核图纸")
    @PostMapping("/update/auditStatus/batch")
    public R<Boolean> updateBatchAuditStatus(@RequestBody List<HomeBuilderDrawingCategoryAuditDTO> auditDTOList) {
        log.info("updateAuditStatus - 批量审核图纸 - auditDTO={}", auditDTOList);
        return R.successDef(homeBuilderDrawingCategoryAuditService.updateBatchAuditStatus(auditDTOList));
    }

    /**
     * 报馆ID删除图纸分类审核.
     *
     * @param reportId      the report id
     * @param drawingSource the drawing source
     * @return the r
     */
    @PostMapping("/remove/reportId")
    public R<Boolean> removeByReportId(@RequestParam("reportId") Long reportId,
                                       @RequestParam("drawingSource") HomeBuilderDrawingCategoryAuditDrawingSourceEnum drawingSource) {
        log.info("removeByReportId - 报馆ID删除图纸分类审核, reportId-={}, drawingSource={}", reportId, drawingSource);
        try {
            return R.successDef(homeBuilderDrawingCategoryAuditService.removeByReportIdAndSource(reportId, drawingSource));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
